#pragma once
#include"common.hpp"
#include <sys/epoll.h>



class epoller{
    // #define SIZE 512
    const int static defaultsize = 512;

    void HandleHelper(int oper,int fd,uint32_t events){
        struct epoll_event ev;
        ev.data.fd = fd;
        ev.events = events;
        int tmpn = epoll_ctl(_epfd,oper,fd,&ev);
        if(tmpn < 0){
            LOG(level::WARING)<<"epoll_ctl false";
        }
        if(oper == EPOLL_CTL_ADD)
            LOG(level::WARING)<<"epoll_ctl add success: "<<fd;
        else if(oper == EPOLL_CTL_DEL)
            LOG(level::WARING)<<"epoll_ctl delect success: "<<fd;
        else
            LOG(level::WARING)<<"epoll_ctl mod success: "<<fd;
        
    }


public:
    epoller():_size(defaultsize){
        _epfd = epoll_create(defaultsize);
    }

    int Wait(int timeout){
        int tmpn = epoll_wait(_epfd,_epev,_size,timeout);
        return tmpn;
    }

    struct epoll_event* GetStructEvents(){
        return _epev;
    }

    void Add(int fd,uint32_t events){
        HandleHelper(EPOLL_CTL_ADD,fd,events);
    }
    void Delect(int fd,uint32_t events){
        HandleHelper(EPOLL_CTL_DEL,fd,events);
    }
    void Mod(int fd,uint32_t events){
        HandleHelper(EPOLL_CTL_MOD,fd,events);
    }


    ~epoller(){}
private:
    int _epfd;
    struct epoll_event _epev[defaultsize];
    int _size;
};